﻿#ifndef __SS_MYDQL_DB_H__
#define __SS_MYDQL_DB_H__

#include "../ssMySQLDefin.h"
#include <string>

namespace StarSeeker
{

class ssMySqlPyQuery
{
public:
	ssMySqlPyQuery();
	ssMySqlPyQuery(ssMySqlPyQuery &rQuery);

	~ssMySqlPyQuery();

	ssMySqlPyQuery& operator=(ssMySqlPyQuery &rQuery);

public:
	void setRes(MYSQL_RES *res);

	int getFieldCount();
	int getRowCount();

	int seekRow(int index);
	void nextRow();

	bool eof();

	const char* getStringValue(int fieldIndex, const char *strNull="");
	const char* getStringValue(const char *fieldName, const char *strNull="");

	int getIntValue(int fieldIndex, const int intNull=0);
	int getIntValue(const char *fieldName, const int intNull=0);

	double getDoubleValue(int fieldIndex, const int intNull=0);
	double getDoubleValue(const char *fieldName, const int intNull=0);

	const char* getOneCell(const char *strNull="");

	int getFieldIndex(const char *fieldName);

	enum_field_types getFieldType(const char *fieldName);
	enum_field_types getFieldType(int fieldIndex);

	bool isFieldTypeString(const char *fieldName);
	bool isFieldTypeString(int fieldIndex);

	const char* getFieldName(int fieldIndex, const char *strNull="");

private:
	void freeRes();

public:
	MYSQL_RES *m_result;
	int m_fieldCount;
	int m_rowCount;
	MYSQL_ROW m_currentRow;
};

class ssMySqlPy
{
public:
	ssMySqlPy();
	~ssMySqlPy();

public:
	bool open(const std::string &host, const std::string &user, const std::string &passwd, const std::string &db, unsigned int port);
	void close();
	bool reopen();
		
	ssMySqlPyQuery& query(const char *sql);
	int excute(const char *sql);
	int lastInsertId();
	void clearResult();
	const char* getError();

private:
	MYSQL *m_connection;
	std::string m_host;
	std::string m_user;
	std::string m_passwd;
	std::string m_db;
	unsigned int m_port;

	ssMySqlPyQuery m_query;
};

}

#endif
